package cn.example.persistencetest;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {

    private TextView tv_content;

    //sql lite 必须的
    private MyDatabaseHelper dbHelper;

    private Button btn_create_database,btn_insert_data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tv_content = findViewById(R.id.tv_content);
        //file
        String inputText = load();
        if (!TextUtils.isEmpty(inputText)){   //一次性进行两种空值的判断
            tv_content.setText(inputText);
            //tv_content.setSelection(inputText.length()); //将光标移动到文本的末尾位置
            tv_content.setTextIsSelectable(false);
            Toast.makeText(this, "Restoring succeeded", Toast.LENGTH_SHORT).show();
        }

        //sql lite3 类处理
        dbHelper=new MyDatabaseHelper(this,"BookStore2.db",null,1);
        btn_create_database= (Button) findViewById(R.id.btn_create_database);
        btn_create_database.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase database = dbHelper.getWritableDatabase();
            }
        });

        btn_insert_data= (Button) findViewById(R.id.btn_insert_data);
        btn_insert_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("name","第一行代码");
                values.put("author","郭霖");
                values.put("pages",545);
                values.put("price",54.5);
                db.insert("Book",null,values);  //插入第一条数据
                values.clear();
                values.put("name","Android群英传");
                values.put("author","徐宜生");
                values.put("pages",421);
                values.put("price",48.9);
                db.insert("Book",null,values);  //插入第二条数据

            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        String text = tv_content.getText().toString();
        sava(text);
    }
    public String load(){
        FileInputStream fileInputStream=null;
        BufferedReader bufferedReader=null;
        StringBuilder builder=new StringBuilder();
        try {
            fileInputStream = openFileInput("data");
            bufferedReader=new BufferedReader(new InputStreamReader(fileInputStream));
            String line="";
            while ((line=bufferedReader.readLine())!=null){
                builder.append(line);
            }

        }  catch (IOException e) {
            e.printStackTrace();
        }
        finally {

            try {
                if (bufferedReader!=null){
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return builder.toString();
    }
    private void sava(String text) {
        FileOutputStream data = null;
        BufferedWriter writer = null;
        try {
            data = this.openFileOutput("data", Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(data));
            writer.write(text);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
